const ScoreModel = require('../models/score')
const UserModel = require('../models/user')
const CourseModel = require('../models/course')

class Chart {
  // home查询
  async getHomeData(ctx) {
      const teacherNum = await UserModel.find({role:1}).countDocuments(true)
      const studentNum = await UserModel.find({role:2}).countDocuments(true)
      const courseNum = await CourseModel.find().countDocuments(true)
      const course = await CourseModel.find()
      return ctx.send({ 
        number:{teacherNum,studentNum,courseNum},
        course 
    })
  }
  // 成绩查询
  async getScore(ctx) {
      let { course,student } = ctx.request.body
      let filter={
        $or:[{
          course
        }]
      }
      if(student){
        filter={
          $or:[{
            student
          }]
        }
      }
        await ScoreModel.find(filter)
          .populate({
            path: 'student',
            select: '_id number username'
          })
          .populate({
            path: 'course',
            select: '_id courseName'
          })
          .then(data => {
            return ctx.send({ scoreLists: data })
          })
          .catch(err => {
            return ctx.sendError('000002')
          })

  }
}

module.exports = new Chart()
